package com.sdp.lease.db.l_wz_type.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.lease.db.l_wz_type.entity.LWzType;
import com.sdp.lease.db.l_wz_type.mapper.LWzTypeMapper;
import com.sdp.lease.db.l_wz_type.service.ILWzTypeService;
import com.sdp.lease.utils.LeaseConstants;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2024-07-25
 */
@Service
public class LWzTypeServiceImpl extends ServiceImpl<LWzTypeMapper, LWzType> implements ILWzTypeService {

	@Override
	public LWzType queryWzTypeByNo(String typeNo) {
		LambdaQueryWrapper<LWzType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LWzType::getTypeNo, typeNo);
		return getOne(queryWrapper,false);
	}

	@Override
	public List<JSONObject> queryWzTypeTableTree() {
		LambdaQueryWrapper<LWzType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.orderByAsc(LWzType::getTypeOrder);
		List<LWzType> typeList = list(queryWrapper);
		List<LWzType> rootList = new ArrayList<LWzType>();
		if(typeList!=null && typeList.size()>0) {
			for(LWzType type : typeList) {
				if(StringUtils.isEmpty(type.getParentNo()) || LeaseConstants.否.getCode().equals(type.getParentNo())) {
					rootList.add(type);
				}
			}
		}
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		JSON.DEFFAULT_DATE_FORMAT = "yyyy-MM-dd HH:mm:ss";
		if(rootList!=null && rootList.size()>0) {
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = JSON.parseObject(JSON.toJSONStringWithDateFormat(rootList.get(i), "yyyy-MM-dd HH:mm:ss", SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat));
				treeList.add(parentObj);
				getChildWzTypeTableTree(parentObj, typeList);
			}
		}
		return treeList;
		
	}
	
	
	@SuppressWarnings("unchecked")
	private void getChildWzTypeTableTree(JSONObject parentObj,List<LWzType> typeList) {
		List<Object> childList = new ArrayList<Object>();
		for(int j=0;j<typeList.size();j++) {
			if(!StringUtils.isEmpty(typeList.get(j).getParentNo()) && parentObj.getString("typeNo").equals(typeList.get(j).getParentNo())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				
				JSONObject childObj = JSON.parseObject(JSON.toJSONStringWithDateFormat(typeList.get(j),"yyyy-MM-dd HH:mm:ss",SerializerFeature.WriteMapNullValue,SerializerFeature.WriteDateUseDateFormat).toString());
				childList.add(childObj);
				getChildWzTypeTableTree(childObj, typeList);
			}
			
		}
	}

	@Override
	public List<JSONObject> queryWzTypeTreeSelect() {
		List<JSONObject> treeList = new ArrayList<JSONObject>();
		List<LWzType> typeList = list();
		List<LWzType> rootList = new ArrayList<LWzType>();
		List<String> rootKeys = new ArrayList<String>();
		if(typeList!=null && typeList.size()>0) {
			for(LWzType type : typeList) {
				if(StringUtils.isEmpty(type.getParentNo()) || LeaseConstants.否.getCode().equals(type.getParentNo())) {
					rootList.add(type);
					rootKeys.add(type.getKeyId());
				}
			}
		}
		
		if(rootList!=null && rootList.size()>0) {
			
			for(int i=0;i<rootList.size();i++) {
				JSONObject parentObj = new JSONObject();
				parentObj.put("title", rootList.get(i).getTypeName());
				parentObj.put("value", rootList.get(i).getTypeNo());
				parentObj.put("key",rootList.get(i).getKeyId());
				parentObj.put("nodeData", rootList.get(i));
				treeList.add(parentObj);
				getChildWzTypeTreeSelect(parentObj, typeList);
			}
		}
		
		return treeList;
	}
	
	
	@SuppressWarnings({ "unchecked" })
	private void getChildWzTypeTreeSelect(JSONObject parentObj,List<LWzType> typeList) {
		List<Object> childList = new ArrayList<Object>();
	
		for(int j=0;j<typeList.size();j++) {
			if(!StringUtils.isEmpty(typeList.get(j).getParentNo()) && parentObj.getString("value").equals(typeList.get(j).getParentNo())) {
				
				if(parentObj.get("children")==null) {
					parentObj.put("children", childList);
				}else {
					childList = (List<Object>) parentObj.get("children");
				}
				
				JSONObject childObj = new JSONObject();
				childObj.put("title", typeList.get(j).getTypeName());
				childObj.put("value", typeList.get(j).getTypeNo());
				childObj.put("key",typeList.get(j).getKeyId());
				childObj.put("nodeData",typeList.get(j));
				childList.add(childObj);
				getChildWzTypeTreeSelect(childObj, typeList);
			}
			
		}
	}

	@Override
	public long queryTypeLevelCountByNo(String typeNo) {
		LambdaQueryWrapper<LWzType> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(LWzType::getTypeNo, typeNo);
		LWzType type = getOne(queryWrapper,false);
		if(type!=null && !StringUtils.isEmpty(type.getTypeLevel())) {
			return type.getTypeLevel().chars().filter(ch->ch==']').count();
		}
		return 0;
	}

}
